package com.expense.app.admin.services.job.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.expense.app.admin.common.BaseService;
import com.expense.app.admin.model.dto.job.JobDTO;
import com.expense.app.admin.model.dto.job.JobImportDTO;
import com.expense.app.admin.model.dto.job.JobListDTO;
import com.expense.app.admin.model.dto.job.JobPageDTO;
import com.expense.app.admin.model.params.job.JobImportParam;
import com.expense.app.admin.model.params.job.zhongkong.JobListParam;
import com.expense.app.admin.model.params.job.zhongkong.JobParam;
import com.expense.app.admin.services.job.ZhongKongJobServ;
import com.expense.common.constant.enums.DeletedStatus;
import com.expense.common.dao.job.ZhongKongJobDao;
import com.expense.common.entity.params.SortParam;
import com.expense.common.exception.PlatformException;
import com.expense.common.model.job.ZhongKongJob;
import com.expense.common.model.auth.AdminUser;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 招聘岗位
 */
@Service
public class ZhongKongJobServImpl extends BaseService implements ZhongKongJobServ {
    private static final Log log = LogFactory.get();

    @Autowired
    ZhongKongJobDao zhongKongJobDao;

    @Override
    public JobListDTO findList(JobListParam param) {
        Map<String,Object> condition = new HashMap<>();
        condition.put("key",param.getKey());
        condition.put("city",param.getCity());
        condition.put("jobType",param.getJobType());
        condition.put("recName",param.getRecName());
        PageQuery pageQuery = initPageQuery(param.getPage(),param.getSort(),new SortParam("create_at",SortParam.DESC));
        pageQuery.setParas(condition);
        zhongKongJobDao.findListPage(pageQuery);
        return new JobListDTO(pageQuery.getList(),pageQuery.getTotalRow());
    }

    @Override
    public JobPageDTO findInitList() {
        List<String> jobTypeList = zhongKongJobDao.findJobTypeList();
        List<String> recList = zhongKongJobDao.findRecList();
        return JobPageDTO.builder().jobTypeList(jobTypeList).recList(recList).build();
    }

    @Override
    public ZhongKongJob findOne(Long id) {
        return zhongKongJobDao.findByIdAndDeleted(id);
    }

    @Override
    public JobDTO findDetail(Long id) {
        ZhongKongJob entity = zhongKongJobDao.findByIdAndDeleted(id);
        return new JobDTO(entity);
    }

    @Override
    public ZhongKongJob saveOne(JobParam param) {
        ZhongKongJob entity = param.getEntity();
        setBaseInfo(entity);
        zhongKongJobDao.saveOne(entity);
        //其他关联处理
        return entity;
    }

    @Override
    public Long deleteOne(Long id) {
        ZhongKongJob entity = zhongKongJobDao.findByIdAndDeleted(id);
        if(entity == null) throw new PlatformException("招聘岗位已不存在");
        entity.setDeleted(DeletedStatus.DELETED);
        setBaseInfo(entity);
        zhongKongJobDao.updateTemplateById(entity);
        //其他关联处理
        return id;
    }

    @Override
    public JobImportDTO importExcel(JobImportParam param) {
        AdminUser loginUser = getCurrentUser();
        List<Map<String,String>> list = param.getMapList();
        List<Map<String,String>> errorList = new ArrayList<>();
        List<Map<String,String>> errorDataList = new ArrayList<>();
        //遍历所有的数据
        if(list != null && list.size()>0) {
            // 清空之前的数据
            if("update".equals(param.getType())){
                zhongKongJobDao.deleteAll(loginUser.getId());
            }
            List<ZhongKongJob> zhongKongJobList = new ArrayList<>();
            for (Map<String, String> map : list) {
                int index = list.indexOf(map);
                String groupName = map.get("主体");
                String recName = map.get("需求公司");
                String depart1 = map.get("一级部门");
                String depart2 = map.get("二级部门");
                String jobTitle = map.get("招聘岗位");
                String jobType = map.get("岗位类别");
//                String needCount = map.get("需求人数");
                String jobDesc = map.get("岗位职责");
                String requirement = map.get("任职要求");
                String workPlace = map.get("工作地点");
//                String isTravel = map.get("是否有常规出差");
//                String isAdvanceInternship = map.get("是否要求提前实习");
//                String destSchool = map.get("目标院校");
//                String destMajor = map.get("目标专业");
//                String eduDegree = map.get("学历要求");
//                String hangzhouNeedCount = map.get("目标城市需求人数:杭州");
//                String nanjingNeedCount = map.get("目标城市需求人数:南京");
//                String wuhanNeedCount = map.get("目标城市需求人数:武汉");
//                String cityNeedCount = map.get("目标城市需求人数:其他城市");
//                String cuSchoolNeedCount = map.get("院校层次需求人数:985/211");
//                String regularSchoolNeedCount = map.get("院校层次需求人数:普通院校");
                String applyUrl = map.get("链接");

                //返回错误记录
                Map<String, String> errorData = new HashMap<>();
                errorData.put("groupName", groupName);
                errorData.put("recName", recName);
                errorData.put("depart1", depart1);
                errorData.put("depart2", depart2);
                errorData.put("jobTitle", jobTitle);
                errorData.put("jobType", jobType);
//                errorData.put("needCount", needCount);
                errorData.put("jobDesc", jobDesc);
                errorData.put("requirement", requirement);
                errorData.put("workPlace", workPlace);
//                errorData.put("isTravel", isTravel);
//                errorData.put("isAdvanceInternship", isAdvanceInternship);
//                errorData.put("destSchool", destSchool);
//                errorData.put("destMajor", destMajor);
//                errorData.put("eduDegree", eduDegree);
//                errorData.put("hangzhouNeedCount", hangzhouNeedCount);
//                errorData.put("nanjingNeedCount", nanjingNeedCount);
//                errorData.put("wuhanNeedCount", wuhanNeedCount);
//                errorData.put("cityNeedCount", cityNeedCount);
//                errorData.put("cuSchoolNeedCount", cuSchoolNeedCount);
//                errorData.put("regularSchoolNeedCount", regularSchoolNeedCount);
                errorData.put("applyUrl", applyUrl);

                ZhongKongJob entity = new ZhongKongJob();

                //主体
                if (StrUtil.isBlank(groupName)) {
                    String reason = "主体不能为空";
                    errorList.add(genErrorMap(index, "主体", groupName, reason));
                    errorDataList.add(genErrorDataMap(errorData, reason));
                    continue;
                }
                if (StrUtil.isBlank(recName)) {
                    String reason = "需求公司不能为空";
                    errorList.add(genErrorMap(index, "需求公司", recName, reason));
                    errorDataList.add(genErrorDataMap(errorData, reason));
                    continue;
                }
                if (StrUtil.isBlank(jobTitle)) {
                    String reason = "招聘岗位不能为空";
                    errorList.add(genErrorMap(index, "招聘岗位", jobTitle, reason));
                    errorDataList.add(genErrorDataMap(errorData, reason));
                    continue;
                }
                if (StrUtil.isBlank(jobType)) {
                    String reason = "岗位类别不能为空";
                    errorList.add(genErrorMap(index, "岗位类别", jobType, reason));
                    errorDataList.add(genErrorDataMap(errorData, reason));
                    continue;
                }
//                if (StrUtil.isBlank(needCount)) {
//                    String reason = "岗位需求人数不能为空";
//                    errorList.add(genErrorMap(index, "需求人数", needCount, reason));
//                    errorDataList.add(genErrorDataMap(errorData, reason));
//                    continue;
//                }
                if (StrUtil.isBlank(applyUrl)) {
                    String reason = "链接地址不能为空";
                    errorList.add(genErrorMap(index, "链接", applyUrl, reason));
                    errorDataList.add(genErrorDataMap(errorData, reason));
                    continue;
                }
//            if(StrUtil.isBlank(workPlace)){
//                String reason = "工作地点不能为空";
//                errorList.add(genErrorMap(index,"工作地点",workPlace,reason));
//                errorDataList.add(genErrorDataMap(errorData,reason));
//                continue;
//            }

                setBaseInfo(entity);
                entity.setGroupName(groupName);
                entity.setRecName(recName);
                entity.setDepart1(depart1);
                entity.setDepart2(depart2);
                entity.setJobTitle(jobTitle);
                entity.setJobType(jobType);
//                entity.setNeedCount(needCount);
                entity.setWorkPlace(workPlace);
                entity.setJobDesc(jobDesc);
                entity.setRequirement(requirement);
//                entity.setIsTravel(isTravel);
//                entity.setIsAdvanceInternship(isAdvanceInternship);
//                entity.setDestSchool(destSchool);
//                entity.setDestMajor(destMajor);
//                entity.setEduDegree(eduDegree);
//                entity.setHangzhouNeedCount(hangzhouNeedCount);
//                entity.setNanjingNeedCount(nanjingNeedCount);
//                entity.setWuhanNeedCount(wuhanNeedCount);
//                entity.setCityNeedCount(cityNeedCount);
//                entity.setCuSchoolNeedCount(cuSchoolNeedCount);
//                entity.setRegularSchoolCount(regularSchoolNeedCount);
                entity.setApplyUrl(applyUrl);
                zhongKongJobList.add(entity);
            }
            if(zhongKongJobList.size()>0) {
                zhongKongJobDao.saveAll(zhongKongJobList);
            }
        }
        JobImportDTO dto = new JobImportDTO();
        dto.setTotalCount(list.size());
        dto.setSuccessCount(list.size() - errorList.size());
        dto.setErrorCount(errorList.size());
        dto.setErrorList(errorList);
        dto.setErrorDataList(errorDataList);
        return dto;
    }


    /**
     * 生成错误返回map
     * @param index 错误所在行索引,从0开始
     * @param field 错误列名
     * @param fieldValue 错误列值
     * @param reason 错误说明
     * @return
     */
    public Map<String,String> genErrorMap(Integer index,String field,String fieldValue,String reason){
        Map<String, String> exit = new HashMap<>();
        exit.put("index", (index+1)+"");
        exit.put("field", field);
        exit.put("val",fieldValue);
        exit.put("reason", reason);
        return exit;
    }
    public Map<String,String> genErrorDataMap(Map<String,String> errorData,String reason){
        errorData.put("reason", reason);
        return errorData;
    }
}
